<?php

/**
 * 继承分页挂件，重新定义分页样式
 */
class MCLinkPager extends CLinkPager
{
	
	public $firstPageCssClass = '';
	public $lastPageCssClass = '';
	public $previousPageCssClass = '';
	public $nextPageCssClass = '';
	public $internalPageCssClass = '';
	public $hiddenPageCssClass = '';
	public $selectedPageCssClass = 'active';
	public $maxButtonCount = 10;
	public $nextPageLabel = '&gt;';
	public $prevPageLabel = '&lt;';
	public $firstPageLabel = '&lt;&lt;';
	public $lastPageLabel = '&gt;&gt;';
	public $header = '';
	public $footer = '';
	public $cssFile = false;
	public $htmlOptions=array('id' => '', 'class' => 'pages');
	
	public function run()
	{
		$this->registerClientScript();
		$buttons=$this->createPageButtons();
		if(empty($buttons))
			return;
		echo $this->header;
		echo CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons));
		echo $this->footer;
	}
	/**
	 * 重载createPageButton
	 * 将class添加至a标签
	 * @see CLinkPager::createPageButton()
	 */
	protected function createPageButton($label,$page,$class,$hidden,$selected)
	{
		if($hidden || $selected)
			$class.=' '.($hidden ? $this->hiddenPageCssClass : $this->selectedPageCssClass);
		return '<li class="'. $class .'">'.CHtml::link($label,$this->createPageUrl($page), array('class' => $class)).'</li>';
	}
	
	protected function createPageButtons()
	{
		//只有一页时也显示分页列表
		$pageCount=$this->getPageCount();
		//if(($pageCount=$this->getPageCount())<=1)
		//	return array();
	
		list($beginPage,$endPage)=$this->getPageRange();
		$currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()
		$buttons=array();
	
		// first page
		$buttons[]=$this->createPageButton($this->firstPageLabel,0,$this->firstPageCssClass,$currentPage<=0,false);
	
		// prev page
		if(($page=$currentPage-1)<0)
			$page=0;
		$buttons[]=$this->createPageButton($this->prevPageLabel,$page,$this->previousPageCssClass,$currentPage<=0,false);
	
		// internal pages
		for($i=$beginPage;$i<=$endPage;++$i)
			$buttons[]=$this->createPageButton($i+1,$i,$this->internalPageCssClass,false,$i==$currentPage);
	
		// next page
		if(($page=$currentPage+1)>=$pageCount-1)
			$page=$pageCount-1;
		$buttons[]=$this->createPageButton($this->nextPageLabel,$page,$this->nextPageCssClass,$currentPage>=$pageCount-1,false);
	
		// last page
		$buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,$this->lastPageCssClass,$currentPage>=$pageCount-1,false);
	
		return $buttons;
	}
}